import { defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import { NaiveUiResolver } from "unplugin-vue-components/resolvers";
import { fileURLToPath, URL } from "node:url";
import Unocss from "unocss/vite";

const baseSrc = fileURLToPath(new URL("./src", import.meta.url));

export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd());

  return {
    plugins: [
      vue(),
      AutoImport({
        // 配置需要自动导入的库
        imports: [
          "vue",
          "vue/macros",
          "vue-router",
          "vue-i18n",
          "@vueuse/core",
          "pinia",
          {
            "naive-ui": [
              "useDialog",
              "useMessage",
              "useNotification",
              "useLoadingBar",
            ],
          },
        ],
        // 生成到的地址
        dts: "types/auto-imports.d.ts",
        // 配置本地需要自动导入的库
        dirs: [
          // pinia状态管理目录
          "src/stores",
          // 自定义组合式api目录
          "src/composables",
        ],
      }),
      Components({
        // 导入naive UI的配置项
        resolvers: [NaiveUiResolver()],
        // 生成类型的地址
        dts: "types/components.d.ts",
      }),
      Unocss(),
    ],
    resolve: {
      alias: {
        "~": baseSrc,
        "~@": baseSrc,
        "@": baseSrc,
      },
    },
    server: {
      proxy: {
        [env.VITE_APP_BASE_API]: {
          target: env.VITE_APP_BASE_URL,
          changeOrigin: true,
          ws: false,
          rewrite: (path) =>
            path.replace(new RegExp(`^${env.VITE_APP_BASE_API}`), ""),
        },
      },
    },
    // 基础配置
    base: "./",
    publicDir: "public",
    build: {
      outDir: "dist",
      assetsDir: "assets",
      assetsInlineLimit: 4096,
      chunkSizeWarningLimit: 2000,
      cssCodeSplit: true,
      sourcemap: false,
      minify: "terser",
      rollupOptions: {
        output: {
          manualChunks: {
            naiveUI: ["naive-ui"],
            antdIcon: ["@vicons/antd"],
            lodash: ["lodash-es", "@vueuse/core"],
            vue: ["vue", "vue-router", "vue-i18n", "pinia"],
          },
        },
      },
      terserOptions: {
        compress: {
          // 生产环境去除console及debug
          drop_console: false,
          drop_debugger: true,
        },
      },
    },
    define: {
      __VUE_I18N_FULL_INSTALL__: false,
      __VUE_I18N_LEGACY_API__: false,
    },
  };
});

/*export default defineConfig({
  plugins: [
    vue(),
    AutoImport({
      // 配置需要自动导入的库
      imports: [
        "vue",
        "vue/macros",
        "vue-router",
        "vue-i18n",
        "@vueuse/core",
        "pinia",
        {
          "naive-ui": [
            "useDialog",
            "useMessage",
            "useNotification",
            "useLoadingBar",
          ],
        },
      ],
      // 生成到的地址
      dts: "types/auto-imports.d.ts",
      // 配置本地需要自动导入的库
      dirs: [
        // pinia状态管理目录
        "src/stores",
        // 自定义组合式api目录
        "src/composables",
      ],
    }),
    Components({
      // 导入naive UI的配置项
      resolvers: [NaiveUiResolver()],
      // 生成类型的地址
      dts: "types/components.d.ts",
    }),
    Unocss(),
  ],
  resolve: {
    alias: {
      "~": baseSrc,
      "~@": baseSrc,
      "@": baseSrc,
    },
  },
  server: {
    proxy: {},
  },
  // 基础配置
  base: "./",
  publicDir: "public",
  build: {
    outDir: "dist",
    assetsDir: "assets",
    assetsInlineLimit: 4096,
    chunkSizeWarningLimit: 2000,
    cssCodeSplit: true,
    sourcemap: false,
    minify: "terser",
    rollupOptions: {
      output: {
        manualChunks: {
          naiveUI: ["naive-ui"],
          antdIcon: ["@vicons/antd"],
          lodash: ["lodash-es", "@vueuse/core"],
          vue: ["vue", "vue-router", "vue-i18n", "pinia"],
        },
      },
    },
    terserOptions: {
      compress: {
        // 生产环境去除console及debug
        drop_console: false,
        drop_debugger: true,
      },
    },
  },
  define: {
    __VUE_I18N_FULL_INSTALL__: false,
    __VUE_I18N_LEGACY_API__: false,
  },
});*/
